// packageA/pages/order-pay/index.js
const WXAPI = require("../../api/goods");
const WXAPI2 = require("../../../api/api");
const dayjs = require("dayjs");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    couponList: [],
    couponId: [],
    id: "",
    orderType: 1, // 1单个下单 2批量下单
    serviceObj: {},
    orderGoodsInfo: {},
    show: false,
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2040, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    currentShowDate: "",
    currentAddress: {
      sId: "",
    },
  },
  toSelectYH(e) {
    const { orgs, orgids } = e.currentTarget.dataset;
    if (!orgids) {
      return false;
    }
    wx.navigateTo({
      url: `/packageA/pages/coupon-list/index?isClick=1&couponList=${JSON.stringify(
        this.data.couponList
      )}&ProductGoods=${JSON.stringify(this.data.data.ProductGoods)}`,
    });
  },
  async getOrderPayDetail() {
    let data;
    if (this.data.orderType == 1) {
      // 直接单个下单
      data = {
        DiscountIds: [this.data.couponId ? this.data.couponId[0] : ""],
        ProductGoods: [
          {
            sShoppingCartId: "",
            sId: this.data.id,
            iCount: 1,
          },
        ],
      };
      // if (!this.data.couponId || this.data.couponId.length == 0) {
      //   delete data.DiscountIds;
      // }
    } else {
      data = {
        DiscountIds: [],
        ProductGoods: [],
      };
      // if (!this.data.couponId || this.data.couponId.length == 0) {
      //   delete data.DiscountIds;
      // }
      for (let i = 0; i < this.data.couponId.length; i++) {
        data.DiscountIds.push(this.data.couponId[i]);
      }
      for (let i = 0; i < this.data.goodsArr.length; i++) {
        data.ProductGoods.push({
          sShoppingCartId: this.data.goodsArr[i].sId,
          sId: this.data.goodsArr[i].sGoodsId,
          iCount: this.data.goodsArr[i].iGoodsCount,
        });
      }
      // 购物车批量下单
    }
    this.setData({
      data,
    });
    let res = await WXAPI.getOrderPayInfo(data);
    if (res.data.code == 0) {
      this.setData({
        orderGoodsInfo: res.data.data,
      });
    }
    console.log('orderGoodsInfo',res.data.data);
    console.log('优惠金额',res.data.data.dDiscountTotalAmount);
  },
  async toOrderService() {
    console.log("this.data.currentAddress", this.data.currentAddress);
    if (!this.data.currentAddress.sId) {
      wx.showToast({
        title: "请选择收货地址！",
        icon: "none",
      });
      return false;
    }
    let prePageObj = {
      sAddressId: this.data.currentAddress.sId,
      ProductOrderCreateInfos: this.data.data,
      ProductOrderAmountByOrgs: this.data.orderGoodsInfo
        .ProductOrderAmountByOrgs,
    };
    console.log("prePageObj", prePageObj);
    wx.navigateTo({
      url: `/packageA/pages/order-pay-type/index?totalPrice=${
        this.data.orderGoodsInfo.dOrderTotalAmount
      }&prePageObj=${JSON.stringify(prePageObj)}`,
    });
  },
  toSelectAddress() {
    wx.navigateTo({
      url: `/packageA/pages/address-manage/index?isSelectAddress=${true}`,
    });
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  selectDateTime() {
    this.setData({
      show: true,
    });
  },
  cancel() {
    this.setData({
      show: false,
    });
  },
  onInput(event) {},

  confirm(event) {
    this.setData({
      currentDate: event.detail,
      currentShowDate: dayjs(event.detail).format("YYYY-MM-DD HH:mm:ss"),
    });
    this.setData({
      show: false,
    });
  },
  // 获取默认地址
  async getDefaultAddress() {
    let res = await WXAPI2.getMyAddressList(this.data.search);
    if (res.data.code == 0 && res.data.data.list.length > 0) {
      let arr = res.data.data.list.filter(
        (item) => item.iDefaultAddress == 1
      )[0];
      console.log("arr", arr);
      this.setData({
        currentAddress: arr && arr.iDefaultAddress == "1" ? arr : {},
      });
    }
  },
  // 获取选择的地址
  async getAddressById(id) {
    let res = await WXAPI2.getAddressInfo(id);
    if (res.data.code == 0) {
      this.setData({
        currentAddress: res.data.data,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("options", options);
    if (options && options.id) {
      // 单个商品下单
      this.setData({
        id: options.id,
        orderType: 1,
      });
    }
    if (options && options.goodsArrStr) {
      // 多个商品下单
      this.setData({
        goodsArr: JSON.parse(options.goodsArrStr),
        orderType: 2,
      });
    }
    this.getDefaultAddress();
    this.getOrderPayDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.addressId) {
      this.getAddressById(this.data.addressId);
    }
    this.getOrderPayDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
